Option Strict Off
Option Explicit On
Imports VB = Microsoft.VisualBasic
Module FileNameGenerator
	'This function generates a unique filename, given a directory it checks to see if the filename exists there.
	'The function returns a filename to the public variable ValidFilename of the format   eg.    2009_02_03_Animation_001.avi
	
	Public Function GenerateFilename(ByRef TargetDirectory As String) As String
		
		
		Dim MonthNum1 As String 'The month of the year. Used to set the initial project name
		Dim DayNum1 As String 'The day of the month. Used to set the initial project name
		Dim FileNumber1 As String 'The string of the file number "001" "002" etc.
		Dim ProjName1 As String
		Dim intFileNumber1 As Short 'The integer of the file number
		
		GenerateFilename = CStr(0) 'Initially set the function to return an error
		intFileNumber1 = 1 'Set the initial filenumber to 1
		MonthNum1 = CStr(Month(Today)) 'Get the month of the year
		DayNum1 = CStr(VB.Day(Today)) 'Get the day of the month
		If Len(MonthNum1) = 1 Then MonthNum1 = "0" & MonthNum1 'If its a single digit month, add a leading zero
		If Len(DayNum1) = 1 Then DayNum1 = "0" & DayNum1 'If its a single digit day, add a leading zero
CheckNameAgain: 
		FileNumber1 = LTrim(Str(intFileNumber1))
		If Len(FileNumber1) = 1 Then FileNumber1 = "00" & FileNumber1
		If Len(FileNumber1) = 2 Then FileNumber1 = "0" & FileNumber1
		ProjName1 = Year(Today) & "_" & MonthNum1 & "_" & DayNum1 & "_Animation_" & FileNumber1 'Set the default filename
		' Check if the file exists
		'UPGRADE_WARNING: Dir has a new behavior. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"'
		If Dir(TargetDirectory & ProjName1 & ".avi") <> "" Then
			intFileNumber1 = intFileNumber1 + 1 'The file exists so increment the number
			GoTo CheckNameAgain 'Recheck again to find a new number
		End If
		
		ValidFilename = ProjName1 & ".avi"
		
		GenerateFilename = CStr(1) 'return that the function has succeeded
		
	End Function
End Module